package com.skiving.cloud.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.admin.entity.SysRole;
import com.skiving.cloud.admin.entity.SysUser;
import com.skiving.cloud.admin.mapper.SysRoleMapper;
import com.skiving.cloud.admin.mapper.SysUserMapper;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysUserRole;
import com.skiving.cloud.admin.mapper.SysUserRoleMapper;
import com.skiving.cloud.admin.service.SysUserRoleService;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * 用户与角色对应关系 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-08
 */
@Component
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>  implements SysUserRoleService {
    @Inject
    private SysRoleMapper sysRoleMapper;
    @Inject
    private SysUserMapper sysUserMapper;
    /**
     * 分页查询用户与角色对应关系
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysUserRole> queryByPage(PageParameter<SysUserRole> parameter){
        Page<SysUserRole> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysUserRole::getId).like(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getUserId())) {
                queryWrapper.and(SysUserRole::getUserId).like(parameter.getParam().getUserId());
            }
            if (Utils.isNotBlank(parameter.getParam().getRoleId())) {
                queryWrapper.and(SysUserRole::getRoleId).like(parameter.getParam().getRoleId());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 查询未设置该角色的用户
     *
     * @param parameter 参数
     * @return 结果
     */
    @Override
    public Page<SysUser> queryNotSetByPage(PageParameter<SysUserRole> parameter) {
        Page<SysUser> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        SysUserRole sysUserRole = parameter.getParam();
        SysRole role = sysRoleMapper.selectOneById(sysUserRole.getRoleId());
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysUser.class).as("u").leftJoin(SysUserRole.class).as("ur").on(
                        wrapper -> wrapper.where(SysUser::getId).eq(SysUserRole::getUserId)
                ).where(SysUser::getTenantId).eq(role.getTenantId()).and(SysUserRole::getId).isNull();
        if(Utils.isNotBlank(sysUserRole.getUsername())){
            queryWrapper.and(SysUser::getUsername).like(sysUserRole.getUsername());
        }
        if(Utils.isNotBlank(sysUserRole.getStaffName())){
            queryWrapper.and(SysUser::getStaffName).like(sysUserRole.getStaffName());
        }
        return sysUserMapper.paginate(page, queryWrapper);
    }

    /**
     * 根据用户id查询用户角色关系
     *
     * @param id 用户id
     * @return 结果
     */
    @Override
    public List<SysUserRole> listByUserId(String id) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysUserRole::getUserId).eq(id);
        return this.list(queryWrapper);
    }
}
